package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.Permission;
import com.itheima.domain.Role;
import com.itheima.factory.MapperFactory;
import com.itheima.mapper.RoleMapper;
import com.itheima.service.RoleService;
import com.itheima.utils.TransactionUtil;
import com.itheima.utils.Uuid;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;


public class RoleServiceImpl implements RoleService {
    @Override
    public List<Role> findAll(String pageNum, String pageSize, String name) {
        SqlSession session = null;
        try {
            // 获取SqlSession对象
            session = MapperFactory.getSqlSession();
            // 获取接口动态实现对象
            RoleMapper mapper = MapperFactory.getMapper(session, RoleMapper.class);
            // 设置分页参数
            int pageN = 1;
            if (StringUtils.isNoneBlank(pageNum)) {
                pageN = Integer.parseInt(pageNum);
            }
            int pageS = 2;
            if (StringUtils.isNoneBlank(pageSize)) {
                pageS = Integer.parseInt(pageSize);
            }
            PageHelper.startPage(pageN, pageS);
            // 操作数据库
            // 处理输入空格情况
            if (StringUtils.isNoneBlank(name)) {
                name = name.replaceAll(" ", "");
            }
            List<Role> roleList = mapper.findAll(name);
            return roleList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("FINDALL ERRO!");
        } finally {
            // 提交事务释放资源
            TransactionUtil.close(session);
        }
    }

    @Override
    public Role findById(String id) {
        SqlSession session = null;
        try {
            // 获取SqlSession对象
            session = MapperFactory.getSqlSession();
            // 获取接口动态实现对象
            RoleMapper mapper = MapperFactory.getMapper(session, RoleMapper.class);
            // 操作数据库
            Role role = mapper.findById(id);
            return role;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("FINDBYID ERRO!");
        } finally {
            // 提交事务释放资源
            TransactionUtil.commit(session);
            TransactionUtil.close(session);
        }
    }

    @Override
    public void save(Role role) {
        SqlSession session = null;
        try {
            // 获取SqlSession对象
            session = MapperFactory.getSqlSession();
            // 获取接口动态实现对象
            RoleMapper mapper = MapperFactory.getMapper(session, RoleMapper.class);
            role.setId(Uuid.getId());
            // 操作数据库
            mapper.save(role);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("SAVE ERRO!");
        } finally {
            // 提交事务释放资源
            TransactionUtil.commit(session);
            TransactionUtil.close(session);
        }
    }

    @Override
    public void delete(String ids) {
        SqlSession session = null;
        try {
            // 获取SqlSession对象
            session = MapperFactory.getSqlSession();
            // 获取接口动态实现对象
            RoleMapper mapper = MapperFactory.getMapper(session, RoleMapper.class);
            String[] idArr = ids.split(",");
            // 操作数据库
            mapper.delete(idArr);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("DELETE ERRO!");
        } finally {
            // 提交事务释放资源
            TransactionUtil.commit(session);
            TransactionUtil.close(session);
        }
    }

    @Override
    public void updatePermission(String roleId,String[] permissionId) {
        SqlSession session = null;
        try {
            // 获取SqlSession对象
            session = MapperFactory.getSqlSession();
            // 获取接口动态实现对象
            RoleMapper mapper = MapperFactory.getMapper(session, RoleMapper.class);
            // 操作数据库
            mapper.deletePermission(roleId);
            if(permissionId != null){
                for(String pid : permissionId)
                    mapper.savePermission(roleId,pid);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("UPDATEPERMISSION ERRO!");
        } finally {
            // 提交事务释放资源
            TransactionUtil.commit(session);
            TransactionUtil.close(session);
        }
    }

}

